<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section A.9.&nbsp; Chapter 9</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-8.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-10.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-APP-A-SECT-9"></a>
<h3 id="631450-805" class="docSection1Title">A.9. Chapter 9</h3>
<dl class="docList">
<dt><br><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 9-1</span></span></p></dt>
<dd><p class="docList">The database connection string is formatted as follows:</p>
<pre>
mysql://<tt><i>db_username</i></tt>:<tt><i>db_password</i></tt>@<tt><i>db_host</i></tt>/<tt><I>db_database</i></tt>:
mysql://joe:my$ql@oreilly.com/survey
</pre><br>

</dd>

<dt><BR><P><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 9-2</span></span></p></dt>
<dd><p class="docList">The database connection requires two steps when you are not using PEAR. First, you must connect to the database. Once you have connected, the survey database is selected.</P>
<pre>
&lt;?php
//set the connection details
$db_host='localhost';
$db_database='test';
$db_username='test';
$db_password='yourpass';
//call mysql_connect to connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database: &lt;br /&gt;". mysql_error());
}
//select the database using mysql_select_db
$db_select = mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: &lt;br /&gt;". mysql_error());
}
?&gt;
</pre><br>

</dd>

<dt><br><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 9-3</span></span></P></dt>
<dd><p class="docList">Add the following to the end of the code from Solution 9-2:</p>
<pre>
&lt;?php
$query = "SELECT * FROM authors";
$result = mysql_query( $query );
if (!$result)
{
die ("Could not query the database: &lt;br /&gt;". mysql_error());
}
while ($result_row = mysql_fetch_row(($result)))
{
       echo 'Author ID: '.$result_row[0] . '&lt;br /&gt;';
       echo 'Title ID: '.$result_row[1] . '&lt;br /&gt; ';
       echo 'Author Name: '.$result_row[2] . '&lt;br /&gt;&lt;br /&gt;';
}
//Close the connection
mysql_close($connection);
?&gt;
</pre><BR>

</dd>

<dt><br><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 9-4</span></span></P></dt>
<dd><p class="docList">The PEAR functions are more compact, and they automate some of the manual work of connecting to and selecting from the database. Because PEAR code is used by many developers, it is less likely to have an error than to have code that's written from scratch.</P></dd>
</dl>

</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-8.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-10.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html>
